package com.sitech.ibnms.c3p.util;

import org.apache.commons.lang3.ArrayUtils;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Created by Administrator on 2015/7/24.
 */
public class DataBaseDialectSqlSessionBeanFactory {

    private static Logger logger = LoggerFactory.getLogger(DataBaseDialectSqlSessionBeanFactory.class);

    public static SqlSessionFactoryBean getDialectSqlSessionFactory(DataSource dataSource, String url, String mapperLocationsPrefix) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] publicResource = null;
        Resource[] dialectResource = null;
        try {
            publicResource = resourcePatternResolver.getResources(mapperLocationsPrefix + ".all.xml");
            if (url.contains("mysql")) {
                dialectResource = resourcePatternResolver.getResources(mapperLocationsPrefix + ".mysql.xml");
            } else {
                dialectResource = resourcePatternResolver.getResources(mapperLocationsPrefix + ".oracle.xml");
            }
        } catch (IOException e) {
            logger.error("获取Mapper配置文件失败", e);
        }
        Resource[] resources = ArrayUtils.addAll(publicResource, dialectResource);
        sqlSessionFactoryBean.setMapperLocations(resources);
        return sqlSessionFactoryBean;
    }

    public static SqlMapClientFactoryBean workFlowDialectSqlMaoClientFactory(DataSource dataSource, String url) {
        SqlMapClientFactoryBean sqlMapClientFactoryBean = new SqlMapClientFactoryBean();
        sqlMapClientFactoryBean.setDataSource(dataSource);

        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        sqlMapClientFactoryBean.setConfigLocation(resourcePatternResolver.getResource("classpath:sql-map-config.xml"));
        Resource[] publicResource = null;
        Resource[] dialectResource = null;
        try {
            publicResource = resourcePatternResolver.getResources("classpath*:com/sitech/**/*Map.all.xml");
            if (url.contains("mysql")) {
                dialectResource = resourcePatternResolver.getResources("classpath*:com/sitech/**/*Map.mysql.xml");
            } else {
                dialectResource = resourcePatternResolver.getResources("classpath*:com/sitech/**/*Map.oracle.xml");
            }
        } catch (IOException e) {
            logger.error("获取Mapper配置文件失败", e);
        }
        Resource[] resources = ArrayUtils.addAll(publicResource, dialectResource);
        sqlMapClientFactoryBean.setMappingLocations(resources);
        return sqlMapClientFactoryBean;
    }
}
